package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.ArtistMapper;
import com.fangyi.yunmusic.mapper.UserIsArtistMapper;
import com.fangyi.yunmusic.mapper.UserMapper;
import com.fangyi.yunmusic.pojo.Artist;
import com.fangyi.yunmusic.pojo.User;
import com.fangyi.yunmusic.pojo.UserIsArtist;
import com.fangyi.yunmusic.pojo.front.end.FEArtist;
import com.fangyi.yunmusic.pojo.vo.ArtistDetailVO;
import com.fangyi.yunmusic.pojo.vo.ArtistVO;
import com.fangyi.yunmusic.service.ArtistService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Id;
import org.n3r.idworker.WorkerIdStrategy;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Service
public class ArtistServiceImpl implements ArtistService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArtistMapper artistMapper;
    @Autowired
    private UserIsArtistMapper userIsArtistMapper;
    @Autowired
    private ResourceConfig resourceConfig;

    @Override
    public ArtistDetailVO getArtistVOByUserId(String userId) {
        Example example = new Example(UserIsArtist.class);
        example.createCriteria().andEqualTo("userId", userId);
        UserIsArtist userIsArtist = userIsArtistMapper.selectOneByExample(example);
        if (userIsArtist == null) {
            throw new YunMusicException("用户还不是歌手");
        }
        Artist artist = artistMapper.selectByPrimaryKey(userIsArtist.getArtistId());
        ArtistDetailVO artArtistDetailVO =new ArtistDetailVO();
        BeanUtils.copyProperties(artist,artArtistDetailVO);
        artArtistDetailVO.setPicUrl(resourceConfig.getArtistPicUrl(artist.getPicUrlId()));
        return artArtistDetailVO;
    }

    @Override
    public void insertArtist(String userId,FEArtist feArtist) {
        User user=userMapper.selectByPrimaryKey(userId);
        if(user==null)
            throw new YunMusicException("用户ID不存在!");
        Artist artist=new Artist();
        BeanUtils.copyProperties(feArtist,artist);
        String artistId=Id.nextString();
        artist.setId(artistId);
        artistMapper.insert(artist);
        UserIsArtist userIsArtist=new UserIsArtist();
        userIsArtist.setArtistId(artistId);
        userIsArtist.setUserId(userId);
        userIsArtist.setId(Id.nextString());
        userIsArtistMapper.insert(userIsArtist);

    }

    @Override
    public void deleteArtist(String artistId) {
        Artist artist=artistMapper.selectByPrimaryKey(artistId);
        if(artist==null)
            throw new YunMusicException("歌手ID不存在");
        Example example=new Example(UserIsArtist.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("artistId",artistId);
        List<UserIsArtist> userIsArtists=userIsArtistMapper.selectByExample(example);
        if(userIsArtists.size()!=1)
            throw new YunMusicException("歌手用户关系表对应关系出错");
        userIsArtistMapper.deleteByPrimaryKey(userIsArtists.get(0).getId());
        artistMapper.deleteByPrimaryKey(artistId);
    }

    @Override
    public PagedResult selectArtist(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Artist> artistList=artistMapper.selectAll();
        return  PagedResult.pagedResult(artistList);

    }

    @Override
    public PagedResult selectArtistVO(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Artist> artistList=artistMapper.selectAll();
        PageInfo<?> pageInfo=new PageInfo<>(artistList);
        int pages= pageInfo.getPages();
        long total=pageInfo.getTotal();
        List<ArtistVO> artistVOS=convertToArtistVOs(artistList);
        return new PagedResult(artistVOS,page,pages,total);
    }

    @Override
    public ArtistDetailVO selectArtistDetailVO(String artistId) {
        Artist artist=artistMapper.selectByPrimaryKey(artistId);
        ArtistDetailVO artArtistDetailVO =new ArtistDetailVO();
        BeanUtils.copyProperties(artist,artArtistDetailVO);
        artArtistDetailVO.setPicUrl(resourceConfig.getArtistPicUrl(artist.getPicUrlId()));
        return artArtistDetailVO;
    }

    public List<ArtistVO> convertToArtistVOs(List<Artist> artistList){
        return  artistList.stream().map(artist -> {
            ArtistVO artistVO=new ArtistVO();
            BeanUtils.copyProperties(artist,artistVO);
            artistVO.setPicUrl(resourceConfig.getArtistPicUrl(artist.getPicUrlId()));
            return artistVO;
        }).collect(toList());
    }
}
